{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers.core import Permute\n",
    "from keras import backend as K\n",
    "import json\n",
    "from collections import OrderedDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=6):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "DATA = OrderedDict()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Permute"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[core.Permute.0] shape [3, 2] -> [2, 3]**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
      "in shape: (3, 2)\n",
      "out shape: (2, 3)\n",
      "out: [0.0, 0.5, 1.0, 0.2, -0.1, 2.0]\n"
     ]
    }
   ],
   "source": [
    "data_in_shape = (3, 2)\n",
    "\n",
    "layer_0 = Input(shape=data_in_shape)\n",
    "layer_1 = Permute((2, 1))(layer_0)\n",
    "model = Model(inputs=layer_0, outputs=layer_1)\n",
    "\n",
    "data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
    "print('in:', data_in)\n",
    "print('in shape:', data_in_shape)\n",
    "arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
    "result = model.predict(np.array([arr_in]))\n",
    "arr_out = result[0]\n",
    "data_out_shape = arr_out.shape\n",
    "print('out shape:', data_out_shape)\n",
    "data_out = format_decimal(arr_out.ravel().tolist())\n",
    "print('out:', data_out)\n",
    "\n",
    "DATA['core.Permute.0'] = {\n",
    "    'input': {'data': data_in, 'shape': data_in_shape},\n",
    "    'expected': {'data': data_out, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[core.Permute.1] shape [2, 3, 4] -> [4, 3, 2]**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "in: [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]\n",
      "in shape: (2, 3, 4)\n",
      "out shape: (4, 3, 2)\n",
      "out: [0.0, 0.0, 1.0, 1.0, 0.5, 0.5, 0.2, 0.2, 2.0, 2.0, -0.1, -0.1, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, -0.1, -0.1, 0.2, 0.2, 2.0, 2.0]\n"
     ]
    }
   ],
   "source": [
    "data_in_shape = (2, 3, 4)\n",
    "\n",
    "layer_0 = Input(shape=data_in_shape)\n",
    "layer_1 = Permute((3, 2, 1))(layer_0)\n",
    "model = Model(inputs=layer_0, outputs=layer_1)\n",
    "\n",
    "data_in = [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]\n",
    "print('in:', data_in)\n",
    "print('in shape:', data_in_shape)\n",
    "arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
    "result = model.predict(np.array([arr_in]))\n",
    "arr_out = result[0]\n",
    "data_out_shape = arr_out.shape\n",
    "print('out shape:', data_out_shape)\n",
    "data_out = format_decimal(arr_out.ravel().tolist())\n",
    "print('out:', data_out)\n",
    "\n",
    "DATA['core.Permute.1'] = {\n",
    "    'input': {'data': data_in, 'shape': data_in_shape},\n",
    "    'expected': {'data': data_out, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[core.Permute.2] shape [1, 6, 4] -> [6, 1, 4]**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "in: [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]\n",
      "in shape: (1, 6, 4)\n",
      "out shape: (6, 1, 4)\n",
      "out: [0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0]\n"
     ]
    }
   ],
   "source": [
    "data_in_shape = (1, 6, 4)\n",
    "\n",
    "layer_0 = Input(shape=data_in_shape)\n",
    "layer_1 = Permute((2, 1, 3))(layer_0)\n",
    "model = Model(inputs=layer_0, outputs=layer_1)\n",
    "\n",
    "data_in = [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]\n",
    "print('in:', data_in)\n",
    "print('in shape:', data_in_shape)\n",
    "arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
    "result = model.predict(np.array([arr_in]))\n",
    "arr_out = result[0]\n",
    "data_out_shape = arr_out.shape\n",
    "print('out shape:', data_out_shape)\n",
    "data_out = format_decimal(arr_out.ravel().tolist())\n",
    "print('out:', data_out)\n",
    "\n",
    "DATA['core.Permute.2'] = {\n",
    "    'input': {'data': data_in, 'shape': data_in_shape},\n",
    "    'expected': {'data': data_out, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[core.Permute.3] shape [1, 3, 4, 2] -> [4, 1, 3, 2]**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "in: [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]\n",
      "in shape: (1, 3, 4, 2)\n",
      "out shape: (4, 1, 3, 2)\n",
      "out: [0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0]\n"
     ]
    }
   ],
   "source": [
    "data_in_shape = (1, 3, 4, 2)\n",
    "\n",
    "layer_0 = Input(shape=data_in_shape)\n",
    "layer_1 = Permute((3, 1, 2, 4))(layer_0)\n",
    "model = Model(inputs=layer_0, outputs=layer_1)\n",
    "\n",
    "data_in = [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]\n",
    "print('in:', data_in)\n",
    "print('in shape:', data_in_shape)\n",
    "arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
    "result = model.predict(np.array([arr_in]))\n",
    "arr_out = result[0]\n",
    "data_out_shape = arr_out.shape\n",
    "print('out shape:', data_out_shape)\n",
    "data_out = format_decimal(arr_out.ravel().tolist())\n",
    "print('out:', data_out)\n",
    "\n",
    "DATA['core.Permute.3'] = {\n",
    "    'input': {'data': data_in, 'shape': data_in_shape},\n",
    "    'expected': {'data': data_out, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### export for Keras.js tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "filename = '../../../test/data/layers/core/Permute.json'\n",
    "if not os.path.exists(os.path.dirname(filename)):\n",
    "    os.makedirs(os.path.dirname(filename))\n",
    "with open(filename, 'w') as f:\n",
    "    json.dump(DATA, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"core.Permute.0\": {\"input\": {\"data\": [0, 0.2, 0.5, -0.1, 1, 2], \"shape\": [3, 2]}, \"expected\": {\"data\": [0.0, 0.5, 1.0, 0.2, -0.1, 2.0], \"shape\": [2, 3]}}, \"core.Permute.1\": {\"input\": {\"data\": [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2], \"shape\": [2, 3, 4]}, \"expected\": {\"data\": [0.0, 0.0, 1.0, 1.0, 0.5, 0.5, 0.2, 0.2, 2.0, 2.0, -0.1, -0.1, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, -0.1, -0.1, 0.2, 0.2, 2.0, 2.0], \"shape\": [4, 3, 2]}}, \"core.Permute.2\": {\"input\": {\"data\": [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2], \"shape\": [1, 6, 4]}, \"expected\": {\"data\": [0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0], \"shape\": [6, 1, 4]}}, \"core.Permute.3\": {\"input\": {\"data\": [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2], \"shape\": [1, 3, 4, 2]}, \"expected\": {\"data\": [0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0], \"shape\": [4, 1, 3, 2]}}}\n"
     ]
    }
   ],
   "source": [
    "print(json.dumps(DATA))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
